package com.kurumi.medical.comparator;

import java.util.Comparator;
import java.util.Map;

import org.springframework.util.StringUtils;

/**
 * Map排序（元素sortCode）
 * @author lyh
 * @version 1.1
 *
 */
public class MapComparator implements Comparator<Map<String, Object>> {

	String sortKey = "sortCode";
	
	/**
	 * default sortCode
	 */
	public MapComparator(){
		
	}
	
	/**
	 * 
	 * @param sortKey
	 */
	public MapComparator(String sortKey){
		if(!StringUtils.isEmpty(sortKey)){
			this.sortKey = sortKey;
		}
		
	}
	
	@Override
	public int compare(Map<String, Object> o1, Map<String, Object> o2) {
		// TODO Auto-generated method stub
		Object sortCodeOjb1 = o1.get(this.sortKey);
		Object sortCodeOjb2 = o2.get(this.sortKey);
		if(sortCodeOjb1 != null && sortCodeOjb2 != null){
			if(sortCodeOjb1 instanceof String && sortCodeOjb2 instanceof String){
				String sortCode1 = (String)sortCodeOjb1;
				String sortCode2 = (String)sortCodeOjb2;
				return sortCode2.compareTo(sortCode1);
			}
		}else if(sortCodeOjb1 != null && sortCodeOjb2 == null){
			return -1;
		}else if(sortCodeOjb1 == null && sortCodeOjb2 != null){
			return 1;
		}
		return 0;
	}
}